package com.example.myapplication.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import com.example.myapplication.enity.IconInfo;

import java.util.ArrayList;
import java.util.List;

public class IconDBHelper extends SQLiteOpenHelper {

    private static final String DB_NAME = "icon.db";
    private static final String TABLE_ICONS_INFO = "icons_info";
    private static final int DB_VERSION = 1;
    private static IconDBHelper mHelper = null;
    private SQLiteDatabase mRDB = null;
    private SQLiteDatabase mWDB = null;

    private IconDBHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    // 利用单例模式获取数据库帮助器的唯一实例
    public static IconDBHelper getInstance(Context context) {
        if (mHelper == null) {
            mHelper = new IconDBHelper(context);
        }
        return mHelper;
    }

    // 打开数据库的读连接
    public SQLiteDatabase openReadLink() {
        if (mRDB == null || !mRDB.isOpen()) {
            mRDB = mHelper.getReadableDatabase();
        }
        return mRDB;
    }

    // 打开数据库的写连接
    public SQLiteDatabase openWriteLink() {
        if (mWDB == null || !mWDB.isOpen()) {
            mWDB = mHelper.getWritableDatabase();
        }
        return mWDB;
    }

    // 关闭数据库连接
    public void closeLink() {
        if (mRDB != null && mRDB.isOpen()) {
            mRDB.close();
            mRDB = null;
        }

        if (mWDB != null && mWDB.isOpen()) {
            mWDB.close();
            mWDB = null;
        }
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // 创建信息表
        String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_ICONS_INFO +
                "(_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," +
                " name VARCHAR NOT NULL," +
                " path VARCHAR NOT NULL);";
        db.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }

    // 查询所有的信息
    public List<IconInfo> queryAllIconsInfo() {
        List<IconInfo> list = new ArrayList<>();

        String sql = " select * from " + TABLE_ICONS_INFO + " LIMIT 2000 OFFSET 0";

        Cursor cursor = mRDB.rawQuery(sql, null);
        while (cursor.moveToNext()) {
            IconInfo info = new IconInfo();
            info.id = cursor.getInt(0);
            info.name = cursor.getString(1);
            info.path = cursor.getString(2);
            list.add(info);
        }
        cursor.close();

        return list;
    }

    // 根据名称查询信息
    public List<IconInfo> queryIconInfoByName(String name) {
        name = name.trim();
        List<IconInfo> list = new ArrayList<>();
        Cursor cursor = mRDB.query(TABLE_ICONS_INFO, null, "name like ?", new String[]{"%"+name+"%"}, null, null, null);
        while (cursor.moveToNext()) {
            IconInfo info = new IconInfo();
            info.id = cursor.getInt(0);
            info.name = cursor.getString(1);
            info.path = cursor.getString(2);
            list.add(info);
        }
        cursor.close();
        return list;
    }

    // 添加多条信息
    public void insertIconsInfos(List<IconInfo> list) {
        // 插入多条记录，要么全部成功，要么全部失败
        try {
            mWDB.beginTransaction();
            for (IconInfo info : list) {
                ContentValues values = new ContentValues();
                values.put("name", info.name);
                values.put("path", info.path);
                mWDB.insert(TABLE_ICONS_INFO, null, values);
            }
            mWDB.setTransactionSuccessful();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            mWDB.endTransaction();
        }
    }
}
